from launch import App
from airtest.core.api import *


class BaseObject:
    """
    对象层基类
    """

    def __init__(self):
        self.poco = App.start()

    def get_element_by_text(self, t):
        try:
            return self.poco(text=t).wait_for_appearance(timeout=10)
        except:
            return None

    def get_element_judge_parent(self, t):
        if self.poco(t[0]).exists():
            return self.poco(t[0])
        else:
            return self.poco(t[1])

    # 按钮文本信息
    def get_daily_btn_name(self, task_name, btn, name):
        return self.poco(text=task_name).sibling(name=btn).child(name)

    def get_element_by_parent(self, t):
        return self.poco(t)

    def get_daily_by_name(self, task_name, btn):
        return self.poco(text=task_name).sibling(name=btn)

    def get_name_and_text(self, info):
        print("通过英雄定位返回的值：", self.poco(text=info[0]))
        if self.poco(text=info[0]).attr("name") == info[1]:
            return self.poco(text=info[0])

    def get_element_path(self, info):
        return self.poco(info[0]).offspring(text=info[1])

    def get_element_parent_offspring(self, info):
        return self.poco(info[0]).offspring(info[1])

    def get_gm_element(self, info):
        return self.poco(info[0]).child(info[1])[info[2]].offspring(info[3])


class BaseOperate:
    """
    操作层基类
    """

    def touch_goods(self, goods):
        touch(Template(goods))
